#include "device_manager.h"
#include <cstddef>
#include <string.h>
extern device_tree_node_base_t _static_device_begin[];
extern device_tree_node_base_t _static_device_end[];
#define IS_STR_EQUALTY(str1,str2) (!strcmp(str1, str2))
#define slist_append(entry, member, itemToAppend) \
{ \
    typeof(entry) node = entry; \
    while(node->member != NULL){node = node->member;} \
    itemToAppend->member = NULL;node->member = itemToAppend; \
}

static device_tree_node_base_t main_root = 
{
    .device_type = DEVICE_TYPE_MAIN_ROOT,
    .root        = NULL,
    .name        = "MainRoot",
    .next_level  = NULL,
    .next_partner = NULL
};

static int device_tree_add_item_to_root(device_tree_node_base_t * root, device_tree_node_base_t *item)
{
    // ASSERT root & item
    if(root->next_level == NULL)
    {
        item->root = root;
        root->next_level = item;
    }
    else {
        slist_append(root, next_partner,item);
    }
    return 0;
}

int device_tree_register_static_device(void)
{
    device_tree_node_base_t *p_dtn = _static_device_begin;
    for(;p_dtn < _static_device_end; p_dtn++)
    {
        device_tree_add_item_to_root(p_dtn->root, p_dtn);
    }
    return 0;
}
#define NODE_MAX_NAME_LENGTH 16
static device_tree_node_base_t *device_tree_get_next_node_from_path(const char *path)
{
    device_tree_node_base_t *root;
    char str_buf[NODE_MAX_NAME_LENGTH];
    int i=0;
    root = &main_root;
    if(path[0] == '/')
    {
        path++;
    }
    while(path[0] != '\0'){
        for(;path[0] != '\0'|| path[0] != '/';i++){
            str_buf[i] = path[0];
            path++;
        }
    }
}

device_tree_node_base_t * device_tree_open_node(const char* device_path)
{
    device_tree_node_base_t *ret = NULL;
    const char *str_ptr = device_path;

    return ret;
}



